package com.irdstudio.tdp.console.api.rest;

import java.util.List;

import com.irdstudio.sdk.beans.core.util.UUIDUtil;
import com.irdstudio.sdk.beans.core.vo.EasyUITreeData;
import com.irdstudio.tdp.console.constant.Constants;
import com.irdstudio.tdp.console.dao.domain.PaasSysTree;
import com.irdstudio.tdp.console.dao.domain.SSubsInfo;
import com.irdstudio.tdp.console.service.facade.PaasAppsInfoService;
import com.irdstudio.tdp.console.service.facade.SSubsInfoService;
import com.irdstudio.tdp.console.service.vo.PaasAppsInfoVO;
import com.irdstudio.tdp.console.service.vo.SSubsInfoVO;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.irdstudio.tdp.console.service.facade.PaasSysTreeService;
import com.irdstudio.tdp.console.service.vo.PaasSysTreeVO;
import com.irdstudio.sdk.beans.core.vo.ResponseData;
import com.irdstudio.sdk.ssm.web.AbstractController;

@RestController
@RequestMapping("/api")
public class PaasSysTreeController extends AbstractController  {
	
	@Autowired
	@Qualifier("paasSysTreeServiceImpl")
	private PaasSysTreeService paasSysTreeService;

	@Autowired
	@Qualifier("paasAppsInfoServiceImpl")
	private PaasAppsInfoService paasAppsInfoService;

	@Autowired
	@Qualifier("sSubsInfoService")
	private SSubsInfoService sSubsInfoService;

	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/paas/sys/trees", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<PaasSysTreeVO>> queryPaasSysTreeAll(
			PaasSysTreeVO vo) {		
		List<PaasSysTreeVO> outputVo = paasSysTreeService.queryAllOwner(vo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/paas/sys/tree/{packageId}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<PaasSysTreeVO> queryByPk(@PathVariable("packageId") String packageId) {		
		PaasSysTreeVO inVo = new PaasSysTreeVO();
				inVo.setPackageId(packageId);
		PaasSysTreeVO outputVo = paasSysTreeService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param paasSysTree
	 * @return
	 */
	@RequestMapping(value="/paas/sys/tree", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody PaasSysTreeVO inPaasSysTreeVo) {		
		int outputVo = paasSysTreeService.deleteByPk(inPaasSysTreeVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inPaasSysTreeVo
	 * @return
	 */
	@RequestMapping(value="/paas/sys/tree", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody PaasSysTreeVO inPaasSysTreeVo) {		
		int outputVo = paasSysTreeService.updateByPk(inPaasSysTreeVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inPaasSysTreeVo
	 * @return
	 */
	@RequestMapping(value="/paas/sys/tree", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertPaasSysTree(@RequestBody PaasSysTreeVO inPaasSysTreeVo) {
		int outputVo = paasSysTreeService.insertPaasSysTree(inPaasSysTreeVo);
		// 创建模型时  还需要在领域服务开发目录下新增一个领域目录及该目录下的“领域服务开发、领域服务编排“两个目录。
		// 03-26 调整为 领域驱动开发 下挂 模型  模型下挂  设计 开发 编排
		if (inPaasSysTreeVo.getPackageType() == Constants.PackageType.Develop.getCode()) {

			PaasSysTreeVO model = new PaasSysTreeVO();
			model.setPackageId(UUIDUtil.getUUID());
			model.setPackageCode(Constants.PackageType.Model.name().toLowerCase());
			model.setPackageName(Constants.PackageType.Model.getName());
			model.setPackageAbvId(inPaasSysTreeVo.getPackageId());
			model.setOrderValue(100);
			model.setPackageType(Constants.PackageType.Model.getCode());
			model.setPackageUrl(String.format("esaas-dmcenter/ModelTableInfoList.html?packageId=%s&&projectId=%s", inPaasSysTreeVo.getPackageId(), inPaasSysTreeVo.getPackageId()));
			paasSysTreeService.insertPaasSysTree(model);

			PaasSysTreeVO dev = new PaasSysTreeVO();
			dev.setPackageId(UUIDUtil.getUUID());
			dev.setPackageCode(Constants.PackageType.Dev.name().toLowerCase());
			dev.setPackageName(Constants.PackageType.Dev.getName());
			dev.setPackageAbvId(inPaasSysTreeVo.getPackageId());
			dev.setOrderValue(200);
			dev.setPackageType(Constants.PackageType.Dev.getCode());
			dev.setPackageUrl(String.format("tdp-console/AppModelInfoList-I01.html?appId=%s", inPaasSysTreeVo.getPackageId()));
			paasSysTreeService.insertPaasSysTree(dev);

			PaasSysTreeVO serve = new PaasSysTreeVO();
			serve.setPackageId(UUIDUtil.getUUID());
			serve.setPackageCode(Constants.PackageType.Serve.name().toLowerCase());
			serve.setPackageName(Constants.PackageType.Serve.getName());
			serve.setPackageAbvId(inPaasSysTreeVo.getPackageId());
			serve.setOrderValue(300);
			serve.setPackageType(Constants.PackageType.Serve.getCode());
			serve.setPackageUrl(String.format("tdp-console/AppModelInfoList-A00.html?appId=%s", inPaasSysTreeVo.getPackageId()));
			paasSysTreeService.insertPaasSysTree(serve);
			// 添加应用
			PaasAppsInfoVO appInfo = new PaasAppsInfoVO();
			appInfo.setAppId(inPaasSysTreeVo.getPackageId());
			appInfo.setAppCode(inPaasSysTreeVo.getPackageCode());
			appInfo.setAppName(inPaasSysTreeVo.getPackageName());
			appInfo.setProjectId(inPaasSysTreeVo.getPackageId());
			appInfo.setProjectCode(inPaasSysTreeVo.getPackageCode());
			appInfo.setProjectName(inPaasSysTreeVo.getPackageName());
			paasAppsInfoService.insertPaasAppsInfo(appInfo);
		}
		return getResponseData(outputVo);
		
	}

	@PostMapping(value = "/paas/sys/tree/nav/{subsCode}")
	public @ResponseBody List<EasyUITreeData> queryTree(@PathVariable("subsCode") String subsCode) {
		SSubsInfoVO param = new SSubsInfoVO();
		param.setSubsCode(subsCode);
		SSubsInfoVO sSubsInfo = sSubsInfoService.queryByPk(param);
		List<EasyUITreeData> treeData = paasSysTreeService.querySSubsInfoTree(sSubsInfo);

		return treeData;
	}
}
